package com.atguigu.servlet;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;
@WebServlet(
        urlPatterns = "/servlet2",
        initParams = {
                @WebInitParam(name="key2a",value="value2a"),
                @WebInitParam(name="key2b",value="value2b")
        }
)
public class Servlet2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = getServletConfig();
        //获取初始配置信息即可
        //根据参数名获取参数值
        String key2a = servletConfig.getInitParameter("key2a");
        System.out.println("key2a:"+key2a);


        //获取所有参数名  iterator
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        //hasMoreElements 判断是否还有元素，如果有返回true，没有返回false
        while (initParameterNames.hasMoreElements()) {
            //nextElement 取出下一个元素，向下移动游标
            String name = initParameterNames.nextElement();
            System.out.println("name:"+name);

        }


        ServletContext servletContext = getServletContext();
        Object ka = servletContext.getAttribute("ka");
        System.out.println(ka);

    }
}
